package com.example.pc.videodemo

import android.telecom.Call
import android.telecom.InCallService
import android.telecom.VideoProfile
import android.util.Log
import com.example.pc.videodemo.MyApplication.Companion.getStack
import com.example.pc.videodemo.emq.MqttManager.Companion.instance

class PhoneCallService : InCallService() {
    private val callback: Call.Callback = object : Call.Callback() {
        override fun onStateChanged(call: Call, state: Int) {
            super.onStateChanged(call, state)
            when (state) {
                Call.STATE_ACTIVE -> {
                    Log.e("wjjj", "通话中")
                    instance?.occupy()
                    val stack = getStack()
                    if (stack.size != 0) {
                        if (stack[0] is MainActivity) {
                            val activity = stack[0] as MainActivity?
                            activity?.showCardCallIng(true)
                        }
                    }
                }
                Call.STATE_DISCONNECTED -> {
                    val stack = getStack()
                    if (stack.size != 0) {
                        if (stack[0] is MainActivity) {
                            val activity = stack[0] as MainActivity?
                            activity?.showCardCallIng(false)
                        }
                    }
                    //  ActivityStack.getInstance().finishActivity(PhoneCallActivity.class);
                    Log.e("wjjj", "通话结束")
                }
            }
        }
    }
    private var callType: CallType? = null
    private var phoneNumber: String? = null

    // 电话进来时会被调用
    override fun onCallAdded(call: Call) {
        super.onCallAdded(call)
        MyApplication.setCall(call)
        call.registerCallback(callback)
        //  PhoneCallManager.call = call; 拿到它可以实现系统的接听挂断的功能
        callType = null
        Log.i("PhoneCallService", "onCallAdded: ")
        if (call.state == Call.STATE_RINGING) {
            callType = CallType.CALL_IN
        } else if (call.state == Call.STATE_CONNECTING) {
            callType = CallType.CALL_OUT
        }
        if (callType != null) {
            val details = call.details
            if (details != null && details.handle != null)
                phoneNumber = details.handle?.schemeSpecificPart


            // 这里开启一个自定义的我们的界面

            /*if (callType == CallType.CALL_IN)
            {
                PhoneActivity.show(this, phoneNumber, new PhoneActivity.OnCallChangeListener()
                {
                    @Override
                    public void onAnswer()
                    {

                        call.answer(STATE_BIDIRECTIONAL);
                    }

                    @Override
                    public void onHangup()
                    {
                        call.disconnect();
                    }
                }, callType == CallType.CALL_IN ? 0 : 1);
            }
            else*/run {
                val stack = getStack()
                if (stack.size == 0) {
                    PhoneActivity.show(this, phoneNumber, object : PhoneActivity.OnCallChangeListener {
                        override fun onAnswer() {
                            call.answer(VideoProfile.STATE_BIDIRECTIONAL)
                        }

                        override fun onHangup() {
                            call.disconnect()
                        }
                    }, if (callType == CallType.CALL_IN) 0 else 1)
                } else if (stack[0] is MainActivity) {
                    val activity = stack[0] as MainActivity?
                    callType?.let {
                        activity?.onPhoneOutCall(phoneNumber, object : PhoneActivity.OnCallChangeListener {
                            override fun onAnswer() {
                                call.answer(VideoProfile.STATE_BIDIRECTIONAL)
                            }

                            override fun onHangup() {
                                call.disconnect()
                            }
                        }, it)
                    }
                }
            }
        }
    }

    // 电话断开时会被调用
    override fun onCallRemoved(call: Call) {
        super.onCallRemoved(call)
        call.unregisterCallback(callback)
        instance?.online()
        val stack = getStack()

//        if (callType == CallType.CALL_IN)
//        {
//
//            if (stack.size() > 1)
//            {
////            while (stack.size() > 1)
//
//                {
//                    stack.get(stack.size() - 1).finish();
//                }
//            }
//        }
//        else
        if (stack.size > 0) {
            if (stack[0] is MainActivity) {
                val activity = stack[0] as MainActivity?
                activity?.onHandupOutCall()
            } else if (stack[0] is PhoneActivity) {
                stack[0]?.finish()
            }
        }

        // PhoneCallManager.call = null;
    }
}

